package com.yc.cloud.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.admin.service.MemberAccountService;
import com.yc.cloud.admin.service.OrderService;
import com.yc.cloud.admin.vo.OrderVo;
import com.yc.cloud.common.basic.dto.BaseQueryParam;
import com.yc.cloud.entity.Order;
import com.yc.cloud.mapper.OrderMapper;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2024-01-18
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    private final MemberAccountService memberAccountService;

    public OrderServiceImpl(MemberAccountService memberAccountService) {
        this.memberAccountService = memberAccountService;
    }

    @Override
    public IPage<OrderVo> list(BaseQueryParam queryParam) {
        val query = new QueryWrapper<Order>();
        IPage<Order> page;
        //排序
//        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
//            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
//                query.orderByDesc(Order.ID);
//            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
//                query.orderByAsc(Order.ID);
//            }
//        }
        //排序
        if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
            query.orderByDesc(queryParam.getSortBy());
        } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
            query.orderByAsc(queryParam.getSortBy());
        }
        //模糊查询
        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
            query.and(queryWrapper -> {
                queryWrapper.like(Order.ORDER_NO, queryParam.getKeyword());
            });
        }
        //分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        val result = baseMapper.selectPage(page, query);
        return result.convert(this::convertVo);
    }

    @Override
    public Order create(Order param) {
        val Order = new Order();
        BeanUtils.copyProperties(param, Order);
        val saveFlag = save(Order);
        return saveFlag ? Order : null;
    }

    @Override
    public Order get(Long id) {
        return getById(id);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public Order update(Long id, Order param) {
        val Order = new Order();
        BeanUtils.copyProperties(param, Order);
        Order.setId(id);
        val updateFlag = updateById(Order);
        return updateFlag ? Order : null;
    }


    @Override
    public OrderVo convertVo(Order Order) {
        if (Order == null) {
            return null;
        }
        val vo = new OrderVo();
        BeanUtils.copyProperties(Order, vo);
        //通过创建人获取用户信息
        val memberAccount = memberAccountService.getById(Order.getCreateBy());
        if (memberAccount != null) {
            vo.setCreateTrueName(memberAccount.getTrueName());
        }
        return vo;
    }
}
